package com.y.web.json;


import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Json2map {
    private static final Pattern SCIENTIFIC_NOTATION = Pattern.compile("([+-]?\\d+(\\.\\d*)?([eE][+-]?\\d+)?)");

    public static String preprocessJson(String json) {
        Matcher matcher = SCIENTIFIC_NOTATION.matcher(json);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            String scientific = matcher.group();
            // 尝试将科学计数法转换为标准的浮点数表示
            // 注意：这里可能需要更复杂的逻辑来处理精度问题
            double value = Double.parseDouble(scientific);
            matcher.appendReplacement(sb, String.valueOf(value));
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    //    public static void main(String[] args) {
//        String str = "{\"fdsafasdf\": \"45234523324\",\"fdsa\":\"3424342\",\"fsadfa\":\"324324\",\"basffd\":6666666666666660.0, \"bsatbasdf\":0E-8," +
//                "\"hasbaedf\":\"\",\"yrbsdz\":\"865786\"}";
////        String cleanJson = str.replaceAll("[\\p{C}\\p{Cc}\\p{Cf}\\p{Co}\\p{Cn}]", "");
////        String parse = str.trim().replaceAll("\\\\", "").replaceAll("\"", "").replaceAll(" ", "");
//
////        ObjectMapper mapper = new ObjectMapper();
////        mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
//
//        // 正则表达式匹配科学计数法表示的数字
//        Pattern scientificNotationPattern = Pattern.compile("[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?");
//        String cleanJson = scientificNotationPattern.matcher(str).replaceAll("\"0.0\"");
//
////        String cleanJso = Json2map.preprocessJson(str);
////        System.out.println(cleanJso);
//
//        System.out.println("======================================================================");
//        Map<String, Object> parse = (Map<String, Object>) JSONUtils.parse(cleanJson);
//
//        System.out.println(parse);
//
//        System.out.println("======================================================================");
//
//        Map<String, Object> parse1 = (Map<String, Object>) JSONObject.parse(cleanJson);
//        System.out.println(parse1);
//
//    }
    public static void main(String[] args) {
        String str = "{\"fdsafasdf\": \"45234523324\",\"fdsa\":\"3424342\",\"fsadfa\":\"324324\",\"basffd\":6666666666666660.0, \"bsatbasdf\":0E-8," +
                "\"hasbaedf\":\"\",\"yrbsdz\":\"865786\"}";

        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);

        // 正则表达式匹配科学计数法表示的数字
        Pattern scientificNotationPattern = Pattern.compile("[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?");
        String cleanJson = scientificNotationPattern.matcher(str).replaceAll("\"0.0\"");

        System.out.println("======================================================================");
        Map<String, Object> parse = (Map<String, Object>) JSONUtils.parse(cleanJson);

        System.out.println(parse);

        System.out.println("======================================================================");

        Map<String, Object> parse1 = (Map<String, Object>) JSONObject.parse(cleanJson);
        System.out.println(parse1);

    }
}
